package chapter07;
import java.util.ArrayList;
import java.util.HashSet;

public class java09_Collection_Set_2 {
    public static void main(String[] args) {
        // TODO 集合 - Collection - Set
        // HashSet :hash + Set
        // Hash : hash 算法 散列算法 （hashcode + equals）
        // 底层数据结构为数组+链表
        HashSet set = new HashSet();

        User9 user1 = new User9();
        user1.id = 1;
        user1.name = "zhangsan";
        User9 user2 = new User9();
        user2.id = 1;
        user2.name = "zhangsan";
        User9 user3 = new User9();
        user3.id = 2;
        user3.name = "lisi";

        set.add(user1);
        set.add(user2);
        set.add(user3);

        System.out.println(set);

    }
}
// 对象地址不同，所以hashset不同
class User9 {
    public int id;
    public String name;

    @Override
    // 类似于内存地址
    public int hashCode() {
        return id;
    }

    @Override
    // 判断两个对象的属性是否完全相同
    public boolean equals(Object obj) {
        if (obj instanceof User9) {
            User9 otherUser = (User9)obj;
            if (otherUser.id == this.id && otherUser.name.equals(this.name)){
                return true;
            }
            return false;
        } else {
            return false;
        }
    }

    public String toString () {
        return "id:"+id+" name:"+name+"";


    }
}
